package com.blockchain.securewallet.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.activities.WalletInfoActivity;
import com.blockchain.securewallet.model.db.CoinWallet;

import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CoinWalletAdapter extends RecyclerView.Adapter<CoinWalletAdapter.WalletItem> {

    private final static String TAG = "CoinWalletAdapter";

    private final LayoutInflater mLayoutInflater;
    private Context context;

    private List<CoinWallet> wallets = null;
    public CoinWalletAdapter(Context context, List<CoinWallet> wallets){
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.wallets = wallets;
    }

    public void setWallets(List<CoinWallet> wallets) {
        this.wallets = wallets;
    }

    @NonNull
    @Override
    public WalletItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new WalletItem(mLayoutInflater.inflate(R.layout.coin_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull WalletItem holder, int position) {
        CoinWallet wallet = wallets.get(position);
        if(wallet.getType() == CoinWallet.TYPE_BIT_COIN) {
            holder.ivIcon.setImageResource(R.drawable.btc);
            holder.tvName.setText(R.string.bit_coin_name);
            holder.tvUnit.setText(R.string.bit_coin_unit);
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            String value = bigDecimal.toPlainString();
            holder.tvCount.setText(value);
        }else if (wallet.getType() == CoinWallet.TYPE_ETH_COIN){
            holder.ivIcon.setImageResource(R.drawable.eth);
            holder.tvName.setText(R.string.eth_coin_name);
            holder.tvUnit.setText(R.string.eth_coin_unit);
            String v = Convert.fromWei(BigDecimal.valueOf(Double.valueOf(wallet.getValue())), Convert.Unit.ETHER).toPlainString();
            holder.tvCount.setText(v);
        }else if (wallet.getType() == CoinWallet.TYPE_LTC_COIN){
            holder.ivIcon.setImageResource(R.drawable.eth);
            holder.tvName.setText(R.string.ltc_coin_name);
            holder.tvUnit.setText(R.string.ltc_coin_unit);
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            String value = bigDecimal.toPlainString();
            holder.tvCount.setText(value);
        }else if (wallet.getType() == CoinWallet.TYPE_BCH_COIN){
            holder.ivIcon.setImageResource(R.drawable.eth);
            holder.tvName.setText(R.string.bch_coin_name);
            holder.tvUnit.setText(R.string.bch_coin_unit);
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            String value = bigDecimal.toPlainString();
            holder.tvCount.setText(value);
        }else if (wallet.getType() == CoinWallet.TYPE_EOS_COIN){
            holder.ivIcon.setImageResource(R.drawable.eth);
            holder.tvName.setText(R.string.eos_coin_name);
            holder.tvUnit.setText(R.string.eos_coin_unit);
            //待补充转单位
//            String v = Convert.fromWei(BigDecimal.valueOf(Double.valueOf(wallet.getValue())), Convert.Unit.ETHER).toPlainString();
            String v = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).toPlainString();
            holder.tvCount.setText(v);
        }

        holder.id  = position;
    }

    @Override
    public int getItemCount() {
        return wallets !=null? wallets.size():0;
    }

    class WalletItem extends RecyclerView.ViewHolder{
        @BindView(R.id.iv_icon)
        ImageView ivIcon;

        @BindView(R.id.tv_name)
        TextView tvName;

        @BindView(R.id.iv_info)
        ImageView ivInfo;

        @BindView(R.id.tv_unit)
        TextView tvUnit;

        @BindView(R.id.tv_count)
        TextView tvCount;

        int id;

        public WalletItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        @OnClick(R.id.iv_info)
        void info(){
            gotoCoinInfo();
        }
        
        @OnClick(R.id.cl_wallet_item)
        void go(){
            gotoCoinInfo();
        }
        
        void gotoCoinInfo(){
//            Intent intent = new Intent("com.blockchain.securewallet.action.COININFO");
//            intent.addCategory("android.intent.category.DEFAULT");
            Intent intent = new Intent();
            intent.setClass(context,WalletInfoActivity.class);
            intent.putExtra(WalletInfoActivity.KEY_WALLET_ID,id);
            CoinWalletAdapter.this.context.startActivity(intent);
        }
    }
}
